package fairygui.editor.handlers
{
   import fairygui.editor.gui.EPackageItem;
   import fairygui.editor.gui.EUIPackage;
   import fairygui.editor.gui.EUIProject;
   import fairygui.editor.utils.UtilsFile;
   import fairygui.editor.utils.UtilsStr;
   
   public class FindReferenceHandler
   {
       
      
      private var _mode:int;
      
      private var _results:Vector.<EPackageItem>;
      
      private var _source:String;
      
      private var _replacing:Boolean;
      
      private var _target:String;
      
      private var _targetPkgId:String;
      
      private var _targetItemId:String;
      
      private var _modified:Boolean;
      
      public function FindReferenceHandler()
      {
         super();
         this._results = new Vector.<EPackageItem>();
      }
      
      public function get results() : Vector.<EPackageItem>
      {
         return this._results;
      }
      
      public function find(param1:String, param2:int, param3:EUIProject) : void
      {
         var _loc4_:EPackageItem = null;
         var _loc5_:Vector.<EUIPackage> = null;
         var _loc6_:EUIPackage = null;
         this._results.length = 0;
         this._mode = param2;
         this._source = param1;
         this._replacing = false;
         if(param2 == 0)
         {
            _loc5_ = param3.getPackageList();
            for each(_loc6_ in _loc5_)
            {
               _loc6_.ensureOpen();
               for each(_loc4_ in _loc6_.resources)
               {
                  this.analyseItem(_loc4_);
               }
            }
         }
         else
         {
            _loc4_ = param3.getItemByURL(param1);
            this.analyseItem(_loc4_);
         }
      }
      
      public function replaceAll(param1:String, param2:String, param3:EUIProject) : void
      {
         var _loc4_:EPackageItem = null;
         this._replacing = true;
         this._target = param2;
         this._targetPkgId = param2.substr(5,8);
         this._targetItemId = param2.substr(13);
         for each(_loc4_ in this._results)
         {
            this.analyseItem(_loc4_);
         }
      }
      
      private function addPropURL(param1:EPackageItem, param2:XML, param3:String, param4:Boolean = false) : void
      {
         var _loc6_:String = null;
         var _loc7_:Array = null;
         var _loc8_:int = 0;
         var _loc9_:Boolean = false;
         var _loc10_:int = 0;
         var _loc11_:Boolean = false;
         var _loc12_:int = 0;
         var _loc13_:String = null;
         var _loc5_:String = param2["@" + param3];
         if(param4)
         {
            if(_loc5_.length == 0)
            {
               return;
            }
            _loc6_ = _loc5_.indexOf(",") != -1?",":"|";
            _loc7_ = _loc5_.split(_loc6_);
            _loc8_ = _loc7_.length;
            _loc9_ = false;
            _loc10_ = 0;
            while(_loc10_ < _loc8_)
            {
               _loc11_ = this.addURL(param1,_loc7_[_loc10_]);
               if(_loc11_ && this._replacing)
               {
                  _loc7_[_loc10_] = this._target;
                  this._modified = true;
                  _loc9_ = true;
               }
               _loc10_++;
            }
            if(_loc9_)
            {
               param2["@" + param3] = _loc7_.join(_loc6_);
            }
         }
         else
         {
            _loc12_ = _loc5_.indexOf(",");
            if(_loc12_ != -1)
            {
               _loc13_ = _loc5_.substr(_loc12_);
               _loc5_ = _loc5_.substr(0,_loc12_);
            }
            else
            {
               _loc13_ = "";
            }
            _loc11_ = this.addURL(param1,_loc5_);
            if(_loc11_ && this._replacing)
            {
               param2["@" + param3] = this._target + _loc13_;
               this._modified = true;
            }
         }
      }
      
      private function addURL(param1:EPackageItem, param2:String) : Boolean
      {
         var _loc3_:EPackageItem = null;
         if(!param2 || param2.length < 14 || !UtilsStr.startsWith(param2,"ui://"))
         {
            return false;
         }
         if(this._mode == 0)
         {
            if(param2 == this._source)
            {
               if(this._results.indexOf(param1) == -1)
               {
                  this._results.push(param1);
               }
               return true;
            }
            return false;
         }
         if(param2 != this._source)
         {
            _loc3_ = param1.owner.project.getItemByURL(param2);
            if(_loc3_ && this._results.indexOf(_loc3_) == -1)
            {
               this._results.push(_loc3_);
            }
         }
         return true;
      }
      
      private function analyseItem(param1:EPackageItem) : void
      {
         var _loc2_:XML = null;
         var _loc3_:Object = null;
         var _loc4_:XMLList = null;
         var _loc5_:XML = null;
         var _loc6_:XML = null;
         var _loc7_:XML = null;
         var _loc8_:String = null;
         var _loc9_:EUIPackage = null;
         var _loc10_:String = null;
         var _loc11_:String = null;
         var _loc12_:String = null;
         var _loc13_:Boolean = false;
         var _loc14_:EPackageItem = null;
         var _loc15_:String = null;
         var _loc16_:Array = null;
         var _loc17_:int = 0;
         var _loc18_:int = 0;
         var _loc19_:Object = null;
         var _loc20_:Array = null;
         var _loc21_:int = 0;
         var _loc22_:Array = null;
         if(param1.type == EPackageItem.COMPONENT)
         {
            _loc2_ = UtilsFile.loadXML(param1.file);
            if(!_loc2_)
            {
               return;
            }
            this._modified = false;
            _loc3_ = _loc2_.displayList.elements();
            for each(_loc5_ in _loc3_)
            {
               _loc10_ = _loc5_.name().localName;
               _loc11_ = _loc5_.@src;
               if(_loc11_)
               {
                  _loc12_ = _loc5_.@pkg;
                  if(_loc12_)
                  {
                     _loc9_ = param1.owner.project.getPackage(_loc12_);
                     if(!_loc9_)
                     {
                        continue;
                     }
                  }
                  else
                  {
                     _loc9_ = param1.owner;
                  }
                  _loc13_ = this.addURL(param1,"ui://" + _loc9_.id + _loc11_);
                  if(_loc13_ && this._replacing)
                  {
                     if(this._targetPkgId != param1.owner.id)
                     {
                        _loc5_.@pkg = this._targetPkgId;
                     }
                     else
                     {
                        delete _loc5_.@pkg;
                     }
                     _loc5_.@src = this._targetItemId;
                     this._modified = true;
                  }
               }
               if(_loc10_ == "component")
               {
                  if(this._mode == 1)
                  {
                     _loc14_ = _loc9_.getItem(_loc11_);
                     if(_loc14_)
                     {
                        this.analyseItem(_loc14_);
                     }
                  }
                  _loc6_ = _loc5_.Button[0];
                  if(_loc6_)
                  {
                     this.addPropURL(param1,_loc6_,"icon");
                     this.addPropURL(param1,_loc6_,"selectedIcon");
                     this.addPropURL(param1,_loc6_,"sound");
                  }
                  _loc6_ = _loc5_.Label[0];
                  if(_loc6_)
                  {
                     this.addPropURL(param1,_loc6_,"icon");
                  }
                  _loc6_ = _loc5_.ComboBox[0];
                  if(_loc6_)
                  {
                     _loc4_ = _loc6_.item;
                     for each(_loc7_ in _loc4_)
                     {
                        _loc8_ = _loc7_.@icon;
                        if(_loc8_)
                        {
                           this.addPropURL(param1,_loc7_,"icon");
                        }
                     }
                  }
               }
               else if(_loc10_ == "loader")
               {
                  this.addPropURL(param1,_loc5_,"url");
               }
               else if(_loc10_ == "list")
               {
                  this.addPropURL(param1,_loc5_,"defaultItem");
                  _loc4_ = _loc5_.item;
                  for each(_loc7_ in _loc4_)
                  {
                     _loc8_ = String(_loc7_.@url);
                     if(_loc8_)
                     {
                        this.addPropURL(param1,_loc7_,"url");
                     }
                     _loc8_ = String(_loc7_.@icon);
                     if(_loc8_)
                     {
                        this.addPropURL(param1,_loc7_,"icon");
                     }
                  }
                  _loc8_ = _loc5_.@scrollBarRes;
                  if(_loc8_)
                  {
                     this.addPropURL(param1,_loc5_,"scrollBarRes",true);
                  }
                  _loc8_ = _loc5_.@ptrRes;
                  if(_loc8_)
                  {
                     this.addPropURL(param1,_loc5_,"ptrRes",true);
                  }
               }
               else if(_loc10_ == "text" || _loc10_ == "richtext")
               {
                  this.addPropURL(param1,_loc5_,"font");
               }
               _loc6_ = _loc5_.gearIcon[0];
               if(_loc6_)
               {
                  this.addPropURL(param1,_loc6_,"values",true);
                  this.addPropURL(param1,_loc6_,"default");
               }
            }
            _loc6_ = _loc2_.Button[0];
            if(_loc6_)
            {
               this.addPropURL(param1,_loc6_,"sound");
            }
            _loc6_ = _loc2_.ComboBox[0];
            if(_loc6_)
            {
               this.addPropURL(param1,_loc6_,"dropdown");
            }
            _loc3_ = _loc2_.transition;
            for each(_loc5_ in _loc3_)
            {
               _loc4_ = _loc5_.item;
               for each(_loc6_ in _loc4_)
               {
                  if(_loc6_.@type == "Sound")
                  {
                     this.addPropURL(param1,_loc6_,"value");
                  }
               }
            }
            this.addPropURL(param1,_loc2_,"designImage");
            this.addPropURL(param1,_loc2_,"hitTestData");
            _loc8_ = _loc2_.@scrollBarRes;
            if(_loc8_)
            {
               this.addPropURL(param1,_loc2_,"scrollBarRes",true);
            }
            _loc8_ = _loc2_.@ptrRes;
            if(_loc8_)
            {
               this.addPropURL(param1,_loc2_,"ptrRes",true);
            }
            if(this._modified)
            {
               UtilsFile.saveXML(param1.file,_loc2_);
               param1.invalidate();
            }
         }
         else if(param1.type == EPackageItem.FONT)
         {
            _loc15_ = UtilsFile.loadString(param1.file);
            if(!_loc15_)
            {
               return;
            }
            _loc16_ = _loc15_.split("\n");
            _loc17_ = _loc16_.length;
            _loc19_ = {};
            _loc18_ = 0;
            while(_loc18_ < _loc17_)
            {
               _loc8_ = _loc16_[_loc18_];
               if(_loc8_)
               {
                  _loc20_ = _loc8_.split(" ");
                  _loc21_ = 1;
                  while(_loc21_ < _loc20_.length)
                  {
                     _loc22_ = _loc20_[_loc21_].split("=");
                     _loc19_[_loc22_[0]] = _loc22_[1];
                     _loc21_++;
                  }
                  _loc8_ = _loc20_[0];
                  if(_loc8_ == "char")
                  {
                     this.addURL(param1,"ui://" + param1.owner.id + _loc19_.img);
                  }
                  else if(_loc8_ == "info")
                  {
                     if(_loc19_.face != undefined)
                     {
                        break;
                     }
                  }
               }
               _loc18_++;
            }
            if(param1.fontTexture)
            {
               _loc14_ = param1.owner.getItem(param1.fontTexture);
               if(_loc14_)
               {
                  this.addURL(param1,"ui://" + _loc14_.owner.id + _loc14_.id);
               }
            }
         }
      }
   }
}
